package com.study.ming.other;

import org.java_websocket.WebSocket;

import java.net.URI;
import java.net.URISyntaxException;

/**
 * @author yh
 * @date 2022/11/2 21:29
 **/
public class Test {

    public static void main(String[] args){
        String message = null;
        String data = "hi!";
        try {
            //实例WebSocketClient对象，并连接到WebSocket服务端
            MyWebSocket client = new MyWebSocket(new URI("ws://127.0.0.1:8080/websocket_book"));
            client.connect();
            //等待服务端响应
            while (!client.getReadyState().equals(WebSocket.READYSTATE.OPEN)) {
                System.out.println("连接中···请稍后");
                Thread.sleep(1000);
            }
            //向WebSocket服务端发送数据
            client.send(data);
            //等待WebSocket服务端响应
            while((message = client.getExcptMessage())==null){
                System.out.println("服务忙等待...");
                Thread.sleep(1000);
            }
            //打印服务端返回的数据
            System.out.println("成功获取数据：" + message);
            //关闭连接
            client.close();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(message);
    }

}

